﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DLTN.Data;
using DLTN.Entities;
using System.Data.SqlClient;
using System.Configuration;

public partial class Admin_Tour_QLChiTietDatTour : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void Bind_Data(int PageNo)
    {
        try
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            if (txtDiachi.Text != null && txtTenCongTy.Text != null)
                WhereClause += " AND DiaChi like N'%" + Common.StandardString(txtDiachi.Text) + "%' and TenCongTy like N'%" + Common.StandardString(txtTenCongTy.Text) + "%'";
            else
                WhereClause += " AND (DiaChi like N'%" + Common.StandardString(txtDiachi.Text) + "%' or TenCongTy like N'%" + Common.StandardString(txtTenCongTy.Text) + "%')";
            DataSet dsTourDat = DataRepository.Provider.ExecuteDataSet("TourDat_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
            gridCatalog.DataSource = dsTourDat.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsTourDat.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }
        catch (Exception)
        {
        }
    }

    /// <summary>
    /// Phân trang Grid view
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLChiTietDatTour.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdTimKiem_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Bind_Data(Pager1.CurrentIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("Admin/Tour/QLChiTietDatTour.aspx.cs", "cmdTimKiem_Click", ex.ToString());
        }
    }

    protected void cmdTimLai_Click(object sender, ImageClickEventArgs e)
    {
        txtDiachi.Text = string.Empty;
        txtTenCongTy.Text = string.Empty;
    }
    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

                HiddenField H_IdCatalog = (HiddenField)e.Row.FindControl("H_IdCatalog");
                e.Row.Attributes.Add("ondblclick", "Javascript:DoubleClick_TourDat('" + H_IdCatalog.Value + "');");
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("Admin/Tour/QLChiTietDatTour.aspx.cs", "gridCatalog_RowDataBound", ex.ToString());
        }
    }
    protected void cmdXoa_Click(object sender, ImageClickEventArgs e)
    {
        int TotalRows = 0;
        int TotalRows1 = 0;
        string WhereClause = "1=1";
        string WhereClause1 = "1=1";
        for (int i = 0; i < gridCatalog.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gridCatalog.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                HiddenField H_IdCatalog = (HiddenField)gridCatalog.Rows[i].Cells[0].FindControl("H_IdCatalog");
                WhereClause += " and IdBooking = " + H_IdCatalog.Value;
                WhereClause1 += " and Id = " + H_IdCatalog.Value;
                TList<ChiTietDatTour> Tlist_ChiTietDatTour = DataRepository.ChiTietDatTourProvider.GetPaged(WhereClause, "", 0, 0, out TotalRows);
                TList<TourDat> Tlist_TourDat = DataRepository.TourDatProvider.GetPaged(WhereClause1, "", 0, 0, out TotalRows1);
                if (Tlist_ChiTietDatTour.Count > 0)
                {
                    for (int j = 0; j < Tlist_ChiTietDatTour.Count; j++)
                    {
                        DataRepository.ChiTietDatTourProvider.Delete(Tlist_ChiTietDatTour[j].Id);
                        string query;
                        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
                        conn.Open();
                        query = "UPDATE Tour SET SoCho = SoCho + 1 where Id = '" + Tlist_TourDat[0].IdTour + "'";
                        SqlCommand sqlCommand = new SqlCommand();
                        sqlCommand.Connection = conn;
                        sqlCommand.CommandText = query;
                        sqlCommand.ExecuteNonQuery();
                        conn.Close();
                    }
                }
                DataRepository.TourDatProvider.Delete(H_IdCatalog.Value);

            }
        }

        Bind_Data(Pager1.CurrentIndex);
    }
    public string GetTieuDe(object Id)
    {
        Tour ent_Tour = DataRepository.TourProvider.GetById(Convert.ToInt32(Id.ToString()));
        return ent_Tour.TieuDe;
    }
    public string GetLoaiThanhToan(object Id)
    {
        int s = Convert.ToInt32(Id.ToString());
        LoaiThanhToan ent_LoaiThanhToan = DataRepository.LoaiThanhToanProvider.GetById(s);
        return ent_LoaiThanhToan.TenHinhThuc;
    }
}